package com.common.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;


import com.models.*;

@Controller
public class AccountController {

	Map<Integer, Account> accountList = new HashMap<Integer, Account>();
	Map<Integer, Messages> messageList = new HashMap<Integer, Messages>();
	
	private static int  accountid = 0;
	private static int messageid = 0;
	
	// SAVE ACCOUNT
	@RequestMapping(value = "/account/save",method=RequestMethod.POST)
	public RedirectView Login(@ModelAttribute Account form,BindingResult errors, HttpServletRequest request, HttpServletResponse response){

		request.getSession(true);
		
		if(form.getAccountid() == -1){
			accountid++;
			form.setAccountid(accountid);
			accountList.put(accountid,form);
		}else{
			accountList.put(form.getAccountid(), form);
		};
		
		request.getSession(true).setAttribute("user_id",form.getAccountid());
        ModelMap map = new ModelMap(); 	
        Iterator iter = accountList.keySet().iterator();
        List<Account> newMap = new ArrayList<Account>();
        while(iter.hasNext()){
            Object key = iter.next();
            if(key != null)
                newMap.add(accountList.get(key));
        }
        for(Account x : newMap){
        	System.out.println(x.getUsername()+"   TEST");
        }
		return new RedirectView("/BlogTest/message");  
    
	}
	
	@RequestMapping(value="/account",method=RequestMethod.GET)
	public String getAccount(HttpServletRequest request){
		System.out.println("3");
        ModelMap map = new ModelMap();
        Iterator iter = accountList.keySet().iterator();
        List<Account> newMap = new ArrayList<Account>();
        while(iter.hasNext()){
            Object key = iter.next();
            if(key != null)
                newMap.add(accountList.get(key));
        }

        
        request.setAttribute("persons", newMap);
        return "blog";
    }
	
	@RequestMapping(value = "/message",method=RequestMethod.POST)	
	public RedirectView SendMessage(Messages form, HttpServletRequest request, HttpServletResponse response){
		System.out.println("1--XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx");
		if(form.getMessageid() == -1){
			messageid++;
			form.setMessageid(messageid);
			messageList.put(messageid,form);
		}else{
			messageList.put(form.getMessageid(), form);
		};
		
		ModelMap map = new ModelMap();
		List<Messages> newMap = new ArrayList<Messages>();

		newMap.add(messageList.get(form.getMessageid()));
		
		map.put("account",newMap);
		return new RedirectView("/BlogTest/message");  
	}
	
	@RequestMapping(value = "/message",method=RequestMethod.GET)	
	public ModelAndView GetMessages(HttpServletRequest request){
		System.out.println("2--XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
		ModelMap map = new ModelMap();
		Iterator iter = messageList.keySet().iterator();
		List<Messages> newMap = new ArrayList<Messages>();
		while(iter.hasNext()){
			Object key = iter.next();
			if(key != null)
				newMap.add(messageList.get(key));
		}
		map.put("messages",newMap);
		return new ModelAndView("blog",map);
	}

	@RequestMapping(value="/delete/{id}")
	public String delete(@PathVariable String id, HttpServletRequest request){
		messageList.remove(Integer.parseInt(id));
		Iterator iter = messageList.keySet().iterator();
		List<Messages> newMap = new ArrayList<Messages>();
		while(iter.hasNext()){
			Object key = iter.next();
			if(key != null)
				newMap.add(messageList.get(key));
		}
		request.setAttribute("messages", newMap);
		return "blog";
	}
	public ModelAndView update(@PathVariable String id){
		ModelMap map = new ModelMap();
		map.put("person", messageList.get(Integer.parseInt(id)));
		return new ModelAndView("blog",map);

	}

}
